package com.yy.jhl.ui.user.presenter;

import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.google.gson.Gson;
import com.yy.common.basebean.BaseResponse;
import com.yy.common.commonutils.LogUtils;
import com.yy.jhl.api.Api;
import com.yy.jhl.api.RxSubscriber;
import com.yy.jhl.bean.ErrorInfo;
import com.yy.jhl.bean.LoginInfo;
import com.yy.jhl.ui.user.contract.LoginContract;
import com.yy.jhl.ui.user.contract.RegisterContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

import static com.yy.jhl.ui.user.contract.LoginContract.*;

/**
 * Created by Ellis.Li on 2017/8/30.
 */

public class LoginPresenter extends LoginContract.Presenter {
    @Override
    public void login(String username, String password,int loginType, String remember_me) {
//        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8")
//                , result.toString());
        //key value请求方式
//        FormBody requestBody = new FormBody.Builder()
//                .add("jsonText", result.toString())
//                .build();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .add("type", loginType+"")
                .build();
        mRxManage.addSubscription(Api.getDefault().login(requestBody), new RxSubscriber<ResponseBody>(mContext, true) {
            @Override
            protected void _onNext(String result) {
                    try {
                        LoginInfo loginInfo = new Gson().fromJson(result,LoginInfo.class);
                        mView.loginSuccess(loginInfo);
                    }catch (Exception e){
                        LogUtils.loge("LOGIN EXCEPTION",e.toString());
                    }
            }

            @Override
            protected void _onError(String message) {
                mView.loginFail();
            }
        });
    }
}
